﻿using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ReflectionDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string rootPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            string path = $"{rootPath}{Path.DirectorySeparatorChar}DLL{Path.DirectorySeparatorChar}ReflectionModel.dll";

            // 程序集
            var assembly = Assembly.LoadFrom(path);
            var version = assembly.GetName().Version.ToString();    // 程序集版本

            // 类
            var types = assembly.GetTypes();
            foreach(var assClass in types)
            {
                Console.WriteLine($"Namespace:{assClass.Namespace},FullName:{assClass.FullName},Name:{assClass.Name}");

                // 方法
                Console.WriteLine($"方法：");
                var methods = assClass.GetMethods();
                foreach (var methodInfo in methods)
                {
                    Console.WriteLine($"方法名:{ methodInfo.Name }，返回类型：{ methodInfo.ReturnType }");

                    // 获取方法下的参数
                    var parameters = methodInfo.GetParameters();
                    foreach (var parameterInfo in parameters)
                    {
                        Console.WriteLine($"参数类型：{parameterInfo.ParameterType}，参数名：{parameterInfo.Name},参数位置：{parameterInfo.Position}，是否输出参数：{parameterInfo.IsOut}");
                    }

                }

                // 属性
                Console.WriteLine($"属性：");
                foreach (var pi in assClass.GetProperties())
                {
                    Console.WriteLine($"属性名：{pi.Name}，属性类型：{pi.MemberType}");
                }

                // 构造函数
                Console.WriteLine($"构造函数：");
                foreach (var ci in assClass.GetConstructors())
                {
                    Console.WriteLine($"构造函数名：{ci.Name}");
                }


                
                



                Console.WriteLine($"");
            }


            // 获取的类型
            var type = assembly.GetType("ReflectionModel.User");
            // 创建类型的实例
            var instance = assembly.CreateInstance("ReflectionModel.User");
            // 设置属性
            type.GetProperty("Id").SetValue(instance, 100, null);
            type.GetProperty("Name").SetValue(instance, "UserName", null);
            // 获取属性值
            var userId = type.GetProperty("Id").GetValue(instance);
            Console.WriteLine($"Id:{userId}");


            // 找到继承某个接口的所有类
            var iType = assembly.GetType("ReflectionModel.IDAL");
            var iTypes = assembly.GetTypes().Where(x => x.GetInterfaces().Contains(iType)).ToArray();
            foreach (var item in iTypes)
            {
                Console.WriteLine(item.FullName);
            }

            // 找到继承某个类的所有子类
            var pType = assembly.GetType("ReflectionModel.DAL");
            var pTypes = assembly.GetTypes().Where(x => x.BaseType == iType).ToArray();
            foreach (var item in pTypes)
            {
                Console.WriteLine(item.FullName);
            }




            Console.ReadKey();
        }
    }
}
